package com.betterwood.dragon.service;

import java.util.ArrayList;
import java.util.List;

import com.betterwood.dragon.dao.KeywordDao;
import com.betterwood.dragon.dao.ResultDao;
import com.betterwood.dragon.dao.SearchDao;
import com.betterwood.dragon.entity.Keyword;
import com.betterwood.dragon.entity.Result;
import com.betterwood.dragon.entity.Search;
import com.betterwood.dragon.entity.Site;
import com.betterwood.dragon.entity.dto.SearchDTO;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import cn.hutool.core.util.StrUtil;
import jdk.internal.jline.internal.Log;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;

@Service(value = "searchService")
@RequiredArgsConstructor
@Slf4j
public class SearchService {
    private final KeywordDao keywordDao;
    private final ResultDao resultDao;
    private final SearchDao searchDao;
    private final SiteService siteService;
    private final WormNest wormNest;

    public String searchAll(SearchDTO search) {
        if (StrUtil.equals(search.getKeywordStr(), "EMPTY")) {
            log.info("EMPTY QUERY");
            return "ok";
        }

        List<Site> sites = siteService.findAll();
        for (Site site : sites) {
            // wormNest.processSite(site, search);
            wormNest.wormGate(site, search);
        }
        return "ok";
    }

    public void deleteBySearchId(String searchId) {
        searchDao.deleteBySearchId(searchId);

    }

    public String searchSingle(SearchDTO search) {
        if (StrUtil.equals(search.getKeywordStr(), "EMPTY")) {
            log.info("EMPTY QUERY");
            return "ok";
        }

        Site site = siteService.findById(search.getSiteNo());

        // wormNest.processSite(site, search);
        wormNest.wormGateTest(site, search);

        return "ok";
    }

    public Search getOneById(String id) {
        return searchDao.getOneById(id);
    }

    public void deleteById(String id) {
        searchDao.deleteById(id);
    }

}
